﻿using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Xml.Linq;
using CharacterCreation.Business.ViewModels;
using CharacterCreation.Data.Models;
using System.Web.Mvc;
using System;

namespace CharacterCreation.Business.Repositories
{
    public class RaceRepository : Repository<VMRace, Race>
    {
        public IEnumerable<SelectListItem> RacesSelectListItem
        {
            get
            {
                var result = this.Get()
                    .Select(r => new SelectListItem() { Selected = false, Value = r.Id.ToString(), Text = r.Name })
                    .ToList();
                result.Insert(0, new SelectListItem() { Selected = true, Text = "Nothing", Value = Guid.Empty.ToString()});
                return result;
            }
        }

        public void Initialize()
        {
            List<VMRace> races = new List<VMRace>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Race.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                races.Add(ObjectMapper.Parse<VMRace>(element));
            }
            this.Add(races);
        }

    }
}
